﻿#include "FileDirDlg.h"
#include <QTimer>
#include <QFileDialog>
#include <QMessageBox>

FileDirDlg::FileDirDlg(QWidget *parent)
	: BaseDlg(parent, "文件通道")
{
	ui.setupUi(&content_view);
	this->base_ui.content->addWidget(&content_view);
	connect(ui.pushButton_Src, &QPushButton::clicked, this, &FileDirDlg::slotBtnSrc);
	connect(ui.pushButton_Dest, &QPushButton::clicked, this, &FileDirDlg::slotBtnDest);
	/*connect(ui.pushButton_Dest, SIGNAL(clicked()), this, SLOT(slotBtnDest()));*/
	/*connect(ui.pushButton_Src, SIGNAL(clicked()), this, SLOT(slotBtnSrc()));
	connect(ui.pushButton_Dest, SIGNAL(clicked()), this, SLOT(slotBtnDest()));
	connect(ui.pushButton_Ok, SIGNAL(clicked()), this, SLOT(slotApply()));

	QTimer *pTimer = new QTimer();
	pTimer->setInterval(2000);
	connect(pTimer, SIGNAL(timeout()), this, SLOT(slotTimeOut()));*/
}

FileDirDlg::~FileDirDlg()
{
}

bool FileDirDlg::get_info(ChannelCreateInfo& info)
{
	info.type |= CHANNEL_PROTO_TYPE_FILE;
	return false;
}
//
void FileDirDlg::slotBtnSrc()
{
	QString strDir = QFileDialog::getExistingDirectory(this, "请选择文件夹", "d:/", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
	ui.lineEdit_Src->setText(strDir);
}
//
void FileDirDlg::slotBtnDest()
{
	QString strDir = QFileDialog::getExistingDirectory(this, "请选择文件夹", "d:/", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
	ui.lineEdit_Dest->setText(strDir);
}
//
//void FileDirDlg::slotApply()
//{
//	m_strDirSrc = ui.lineEdit_Src->text();
//	m_strDirDest = ui.lineEdit_Dest->text();
//
//	this->hide();
//}
//
//void FileDirDlg::slotTimeOut()
//{
//	if (!m_strDirSrc.isEmpty())
//	{
//		DetectFile(m_strDirSrc);
//	}
//}
//
//QFileInfoList FileDirDlg::GetFileList(const QString& path)
//{
//	QDir dir(path);
//	QFileInfoList file_lst = dir.entryInfoList(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
//	QFileInfoList folder_lst = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
//	for (int i=0; i< folder_lst.size(); ++i)
//	{
//		QString name = folder_lst.at(i).absoluteFilePath();
//		QFileInfoList child_file_lst = GetFileList(name);
//		file_lst.append(child_file_lst);
//	}
//	return file_lst;
//}
//
//void FileDirDlg::DetectFile(const QString& filePath)
//{
//	std::map<QString, short>::iterator itr = m_mapDeleteFiles.begin();
//	while (itr != m_mapDeleteFiles.end())
//	{
//		int nTimes = itr->second + 1;
//		if (nTimes > 2)
//		{
//			//删除文件
//			itr = m_mapDeleteFiles.erase(itr);
//			continue;
//		}
//		else
//		{
//			m_mapDeleteFiles[itr->first] = nTimes;
//		}
//		++itr;
//	}
//
//	QFileInfoList lstFile = GetFileList(filePath);
//	for (int i=0; i<lstFile.size(); ++i)
//	{
//		QFileInfo info(lstFile[i]);
//		QString strFileName = info.absoluteFilePath();
//		QFile file(strFileName);
//		if (!file.open(QFile::ReadOnly))
//		{
//			QMessageBox::information(this, "提示", "文件打开失败");
//			continue;
//		}
//
//		int nSize = file.size();
//		QString strContent = file.readAll().toStdString().c_str();
//		file.close();
//		int ret = DataRelay::GetInstance()->SendData(ui.comboBox_SendDataName->currentText(), strContent, nSize);
//		if (ret > 0)
//		{
//			m_mapDeleteFiles.insert(std::make_pair(strFileName, 1));
//		}
//	}
//}
